package cn.com.blueInfo.utils.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;

/**
 * 请求数据流
 * @ClassName: RequestPayload
 * @author suxch
 * @date 2018年7月11日  下午12:06:46
 */
@Deprecated
public class RequestPayload {

    /**
     * 请求数据流解析
     * @Title: analysis
     * @param request
     * @return JSONObject
     * @throws
     */
    public static JSONObject analysis(HttpServletRequest request) {
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuffer.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuffer.append("");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        String jsonStr = stringBuffer.toString();
        if (jsonStr.indexOf("=%7B") > 0) {
            try {
                jsonStr = URLDecoder.decode(jsonStr, "UTF-8");
                jsonStr = jsonStr.substring(jsonStr.indexOf("=") + 1);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        jsonStr = jsonStr.replace("*", "%");
        return JSON.parseObject(jsonStr);
    }
}

